import {useCallback, useLayoutEffect, useRef} from 'react'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useEvent<T extends (...args: any[]) => any>(handler: T) {
  const handlerRef = useRef<T>(null)

  // In a real implementation, this would run before layout effects
  useLayoutEffect(() => {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    handlerRef.current = handler
  })

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-expect-error
  return useCallback<T>((...args) => {
    // In a real implementation, this would throw if called during render
    const fn = handlerRef.current
    return fn?.(...args)
  }, [])
}
